import {
  UserService
} from "../../services/user_service";

// pages/profile/profile.js
Page({
  data: {
    avatarUrl: '',
    nickname: ''
  },

  onShow: function (options) {
    const app = getApp();
    this.setData({
      avatarUrl: app.globalData.userInfo.avatarUrl,
      nickname: app.globalData.userInfo.nickname
    })
  },

  getUserInfo() {
    if (!(getApp().globalData.userInfo.avatarUrl)) {
      wx.getUserProfile({
        desc: '获取您的头像，昵称',
        success: (res) => {
          let userService = new UserService();
          userService.updateUserInfo({
            nickname: res.userInfo.nickName,
            avatarUrl: res.userInfo.avatarUrl,
          }).then(res => {
            const app = getApp();
            var userInfo = Object.assign(res.data, app.globalData.userInfo);
            app.globalData.userInfo = userInfo;
            wx.setStorageSync('userInfo', JSON.stringify(userInfo));
          });
          this.setData({
            avatarUrl: res.userInfo.avatarUrl,
            nickname: res.userInfo.nickName
          })
        }
      })
    }
  },

  goGuidePage() {
    wx.navigateTo({
      url: '/pages/guide/guide',
    })
  },

  goServicePage() {
    wx.navigateTo({
      url: '/pages/customer/customer',
    })
    
  }


})